from django.shortcuts import render,HttpResponse
import requests,json,random
from selenium import webdriver


headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "text/plain;charset=UTF-8",
"Host": "www.chinaunicom.com.cn",
"Pragma": "no-cache",
"Referer": "http://www.chinaunicom.com.cn/news/list202303.html",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",   
}

def moth_chuli(n):
        if (n<10):
            a = "0"+str(n)
            return a
        else:
            return a

def time(req):
    print(req.method)
    return render(req,"time.html")
    
def news(req):
    
    time1 = int(req.POST.get('time1'))#获取起始年
    moth1 = int(req.POST.get('moth1'))#获取起始月
    time2 = int(req.POST.get('time2'))#获取终止年
    moth2 = int(req.POST.get('moth2'))#获取终止月
    print(time1,time2,moth1,moth2)
    driver = webdriver.PhantomJS(executable_path=f"D:\\PhantomJS\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
    driver.get(url=f"http://www.chinaunicom.com.cn/api/article/NewsByIndex/1/{time1}/{moth1}/news")
    # 判断 获取要爬取的url列表 -返回data
    if (time1==time2):
        time_first= [f"http://www.chinaunicom.com.cn/api/article/NewsByIndex/1/{time1}/{moth_chuli(b)}/news" for b in range(moth1,moth2+1)]
        time_last = []
    else:
        time_first= [f"http://www.chinaunicom.com.cn/api/article/NewsByIndex/1/{time1}/{moth_chuli(b)}/news" for b in range(moth1,13) if(b<10)]
        time_last= list()
        for i in range(time1+1,time2+1):
            for j in range(1,13):
                url = f"http://www.chinaunicom.com.cn/api/article/NewsByIndex/1/{i}/{moth_chuli(j)}/news"
                if(i == time2)&(j==moth2+1):
                    break
                time_last.append(url)
    time = time_first+time_last
    print(time)
    data = list()
    for url in time:
        res = requests.get(url=url,headers=headers)
        data += res.json()
    # 消重 消除url中相同的链接
    
    new_list = []
    new_list_id = []

    for i in data:
        if(i['news_id'] not in new_list_id):
            new_list.append(i)
            new_list_id.append(i['news_id'])
    
    data_list = new_list        

    return render(req,"news1.html",{'n1':data_list})
